package sk.uniza.fri.cerviix.server.network.messages.server;

import java.io.IOException;
import java.util.LinkedList;
import java.util.List;
import java.util.Queue;

import sk.uniza.fri.cerviix.server.network.MessageType;
import sk.uniza.fri.cerviix.server.network.ServerMessage;

public class UpdateMessage extends ServerMessage
{
	private List<Struct> structs = new LinkedList<Struct>();
	
	public void add(int playerId, int score, boolean alive, float x, float y)
	{
		structs.add(new Struct(playerId, score, alive, x, y));
	}
	
	@Override
	public void send() throws IOException
	{
		setHeader(MessageType.UPDATE);
		
		stream.writeInt(structs.size());
		
		for (Struct struct : structs)
		{
			stream.writeInt(struct.playerId);
			stream.writeInt(struct.score);
			stream.writeBoolean(struct.alive);
			
			if (struct.alive)
			{
				stream.writeFloat(struct.x);
				stream.writeFloat(struct.y);
			}
		}
	}
	
	private static class Struct
	{
		public final int playerId, score;
		public final boolean alive;
		public final float x, y;
		
		public Struct(int playerId, int score, boolean alive, float x, float y)
		{
			this.playerId = playerId;
			this.score = score;
			this.alive = alive;
			this.x = x;
			this.y = y;
		}
	}
}
